#include "module_port_property.h"
#include "ui_module_port_property.h"

#include <QDebug>

module_port_property::module_port_property(PortAttribute *data, QWidget *parent) :
    QFrame(parent),
    ui(new Ui::module_port_property)
{
    ui->setupUi(this);

    this->setObjectName("Property");

    this->data = data;

    this->init();

#if (QT_VERSION <= QT_VERSION_CHECK(5,13,0))
    ui->lineEdit_Name->setValidator(new QRegExpValidator(QRegExp("^[a-zA-Z]([a-zA-Z]|[_]|[0-9])+$")));
    ui->lineEdit_dataSize->setValidator(new QRegExpValidator(QRegExp("^[1-9][0-9]+$")));
#else //qt6版本
    ui->lineEdit_Name->setValidator(new QRegularExpressionValidator(QRegularExpression("^[a-zA-Z]([a-zA-Z]|[_]|[0-9])+$")));
    ui->lineEdit_dataSize->setValidator(new QRegularExpressionValidator(QRegularExpression("^[1-9][0-9]+$")));
    ///---------------------------------------------------------------
#endif
}

module_port_property::~module_port_property()
{
    delete ui;
}

void module_port_property::set_InOut(QString InOut)
{
//    ui->label_InOut->setText(InOut);
//    ui->comboBox_inout->currentIndexChanged(InOut)
    if (InOut == "Input")
        ui->comboBox_inout->setCurrentIndex(0);
    else if (InOut == "Output")
        ui->comboBox_inout->setCurrentIndex(1);
    else if (InOut == "InOut")
        ui->comboBox_inout->setCurrentIndex(2);
}

void module_port_property::set_name(QString name)
{
    ui->lineEdit_Name->setText(name);
}

void module_port_property::init()
{
    ui->lineEdit_Name->setText(data->name);
    this->set_InOut(data->type);
    ui->lineEdit_dataSize->setText(QString::number(this->data->dataSize));
}

void module_port_property::on_comboBox_dataType_currentIndexChanged(const QString &arg1)
{
    qDebug() << "select combobox" << arg1;
    this->data->datatype = arg1;
}


void module_port_property::on_comboBox_inout_currentIndexChanged(const QString &arg1)
{
    qDebug() << "select combobox" << arg1;
    this->data->type = arg1;
}


void module_port_property::on_lineEdit_dataSize_textChanged(const QString &arg1)
{
    if (arg1.isNull() || arg1.isEmpty())
        this->data->dataSize = 1;
    else
        this->data->dataSize = arg1.toInt();
}

